//
//  WLOrderViewController.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/23.
//

import UIKit
import SwiftyJSON
import HandyJSON

class OrderViewController: ZsBaseViewController, ClickMenuDelegate, UIScrollViewDelegate, OrderSeleectCityDelegate {
    
    var allListVC:OrderStateController!
    var waitTakeOrderVC = OrderStateController()
    var waitCollectListVC = OrderStateController()
    var waitSortingVC = OrderStateController()
    var storageSortingVC = OrderStateController()
    var waitSendOrderVC = OrderStateController()
    var transportationVC = OrderStateController()
    var waitDeliveryVC = OrderStateController()
    var signedVC = OrderStateController()
    var stationOrderVC = OrderStateController()
    var airportOrderVC = OrderStateController()
    var cancelOrderVC = OrderStateController()

    var pointView:UIView! = nil
    var menuStateAry:Array<String>!
    var controllers:Array<UIViewController>!
    
    var menu:CustomPopupMenu! = nil
    var orderScreenSender:OrderSeleectCity! = nil
    var orderScreenCollect:OrderSeleectCity! = nil

    var currentIndexPage:Int = 0
    //是否搜索过 点击X的时候如果没有搜索过 当前界面不用重新获取接口
    var isSearched:Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        self.initObject()
        self.initItem()
        self.navigationItem.titleView?.sizeToFit()
        self.view.backgroundColor = UIColor.init(hexString: "#F5F6F5")
        self.navigationItem.titleView = self.setupSearchHeaderView()
        
        NotificationCenter.default.addObserver(self, selector: #selector(refreshList), name: Notification.Name(rawValue:ActionHeader.Order_orderOperation), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(refreshOrderTitleCount), name: Notification.Name(rawValue: ActionHeader.Order_titleCount), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(languageNotification), name: Notification.Name(rawValue: "languageNotification"), object: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.getOrderScreenSender().isSelect || self.getOrderScreenCollect().isSelect {
            self.pointView.isHidden = false
        }
    }
    
    func initObject()  {
        var titles:Array<String>!
        let userType = WLUserModel.getUserType()
        controllers = Array()
        menuStateAry = Array()
        
        allListVC = OrderStateController()
        allListVC.orderState = Constants.order_All
        allListVC.orderVC = self
        self.addChild(allListVC)
        controllers.append(allListVC!)
        menuStateAry.append(Constants.order_All)
        
        if userType == "1" {
            var listPageViews = Array<UIView>()
            listPageViews.append(allListVC.view!)
            let roleType = WLUserModel.getRoleType()
            
            for str in roleType {
                if str as! Int  == 3 {
                    waitCollectListVC = OrderStateController()
                    waitCollectListVC.orderState = Constants.order_waitColloct
                    self.addChild(waitCollectListVC)
                    waitCollectListVC.orderVC = self
                    controllers.append(waitCollectListVC)
                    listPageViews.append(waitCollectListVC.view)
                    menuStateAry.append(Constants.order_waitColloct)
                } else if str as! Int  == 4 {
                    waitSortingVC = OrderStateController()
                    storageSortingVC = OrderStateController()

                    waitSortingVC.orderState = Constants.order_waitSorting
                    storageSortingVC.orderState = Constants.order_storage
                    
                    waitSortingVC.orderVC = self
                    storageSortingVC.orderVC = self
                    
                    self.addChild(waitSortingVC)
                    self.addChild(storageSortingVC)
                    
                    listPageViews.append(waitSortingVC.view)
                    listPageViews.append(storageSortingVC.view)
                    
                    menuStateAry.append(Constants.order_waitSorting)
                    menuStateAry.append(Constants.order_storage)
                    
                    controllers.append(waitSortingVC)
                    controllers.append(storageSortingVC)

                } else if str as! Int  == 5 {
                    waitDeliveryVC = OrderStateController()
                    signedVC = OrderStateController()
                    stationOrderVC = OrderStateController()
                    airportOrderVC = OrderStateController()
                    
                    waitDeliveryVC.orderState = Constants.order_waitDelivery
                    signedVC.orderState = Constants.order_signed
                    stationOrderVC.orderState = Constants.order_station
                    airportOrderVC.orderState = Constants.order_air
                    
                    waitDeliveryVC.orderVC = self
                    signedVC.orderVC = self
                    stationOrderVC.orderVC = self
                    airportOrderVC.orderVC = self
                    
                    self.addChild(waitDeliveryVC)
                    self.addChild(signedVC)
                    self.addChild(stationOrderVC)
                    self.addChild(airportOrderVC)
                    
                    listPageViews.append(waitDeliveryVC.view)
                    listPageViews.append(signedVC.view)
                    listPageViews.append(stationOrderVC.view)
                    listPageViews.append(airportOrderVC.view)
                    
                    menuStateAry.append(Constants.order_waitDelivery)
                    menuStateAry.append(Constants.order_signed)
                    menuStateAry.append(Constants.order_station)
                    menuStateAry.append(Constants.order_air)
                    
                    controllers.append(waitDeliveryVC)
                    controllers.append(signedVC)
                    controllers.append(stationOrderVC)
                    controllers.append(airportOrderVC)
                }
            }
            self.clickMenuView.setDataSourceAction(dataSource: self.getOrderStr())
            self.listPageView.setViews(views: listPageViews)
        } else {
            waitTakeOrderVC = OrderStateController()
            waitCollectListVC = OrderStateController()
            waitSortingVC = OrderStateController()
            storageSortingVC = OrderStateController()
            waitSendOrderVC = OrderStateController()
            
            transportationVC = OrderStateController()
            waitDeliveryVC = OrderStateController()
            signedVC = OrderStateController()
            stationOrderVC = OrderStateController()
            airportOrderVC = OrderStateController()
            
            cancelOrderVC = OrderStateController()

            waitTakeOrderVC.orderState = Constants.order_waitReceive
            waitCollectListVC.orderState = Constants.order_waitColloct
            waitSortingVC.orderState = Constants.order_waitSorting
            storageSortingVC.orderState = Constants.order_storage
            waitSendOrderVC.orderState = Constants.order_waitSend
            
            transportationVC.orderState = Constants.order_transit
            waitDeliveryVC.orderState = Constants.order_waitDelivery
            signedVC.orderState = Constants.order_signed
            stationOrderVC.orderState = Constants.order_storage
            airportOrderVC.orderState = Constants.order_air
            
            cancelOrderVC.orderState = Constants.order_cancel

            waitTakeOrderVC.orderVC = self;
            waitCollectListVC.orderVC = self;
            waitSortingVC.orderVC = self;
            waitDeliveryVC.orderVC = self;
            waitSendOrderVC.orderVC = self;
            
            transportationVC.orderVC = self;
            signedVC.orderVC = self;
            stationOrderVC.orderVC = self;
            airportOrderVC.orderVC = self;
            cancelOrderVC.orderVC = self;
            
            storageSortingVC.orderVC = self;
            
            titles = [ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_All),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitReceive),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitCollect),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitSorting),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitSender),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_Storageing),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_carriage),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_delivery),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_signed),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_stationed),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_airported),
                      ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_canceled)]
            self.clickMenuView.setDataSourceAction(dataSource: titles)
            
            self.listPageView.setViews(views: [allListVC.view,waitTakeOrderVC.view,waitCollectListVC.view,waitSendOrderVC.view,waitSortingVC.view,storageSortingVC.view,transportationVC.view,waitDeliveryVC.view,signedVC.view,stationOrderVC.view,airportOrderVC.view,cancelOrderVC.view]) 
            
            menuStateAry.append(Constants.order_waitReceive)
            menuStateAry.append(Constants.order_waitColloct)
            menuStateAry.append(Constants.order_waitSorting)
            menuStateAry.append(Constants.order_waitSend)
            menuStateAry.append(Constants.order_storage)
            menuStateAry.append(Constants.order_transit)
            menuStateAry.append(Constants.order_waitDelivery)
            menuStateAry.append(Constants.order_signed)
            menuStateAry.append(Constants.order_storage)
            menuStateAry.append(Constants.order_air)
            menuStateAry.append(Constants.order_cancel)
            
            controllers.append(waitTakeOrderVC)
            controllers.append(waitCollectListVC)
            controllers.append(waitSendOrderVC)
            controllers.append(waitSortingVC)
            controllers.append(storageSortingVC)
            controllers.append(transportationVC)
            controllers.append(waitDeliveryVC)
            controllers.append(signedVC)
            controllers.append(stationOrderVC)
            controllers.append(airportOrderVC)
            controllers.append(cancelOrderVC)
        }
        self.view.addSubview(self.clickMenuView)
        self.view.addSubview(self.listPageView)
        
    }
    func initItem()  {
        self.orderSearchView.placeholder = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_search_order)
        let rightItemView = UIView()
        rightItemView.frame = CGRect.init(x: 0, y: 0, width: 80, height: 20)
        
        let rightBtn = UIButton.init(type: .custom)
        rightBtn.frame = CGRect.init(x: 0, y: 0, width: 80, height: 20)
        rightBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Filtrate), for: .normal)
        rightBtn.addTarget(self, action: #selector(pushPayList), for: .touchUpInside)
        rightBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        rightBtn.setImage(UIImage.init(named: "order_screen"), for: .normal)
        rightBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
        rightItemView.addSubview(rightBtn)
        
        pointView = UIView.init(frame: CGRect.init(x: 60, y: 0, width: 5, height: 5))
        pointView.layer.masksToBounds = true
        pointView.layer.cornerRadius = 2.5
        pointView.backgroundColor = UIColor.red
        pointView.isHidden = true
        rightItemView.addSubview(pointView)
        
        let rightTwoBtn = UIButton.init(type: .custom)
        rightBtn.frame = CGRect.init(x: 0, y: 0, width: 40, height: 40)
        rightTwoBtn.setImage(UIImage.init(named: "order_Scan"), for: .normal)
        
        let right1 = UIBarButtonItem.init(customView: rightTwoBtn)
        let right2 = UIBarButtonItem.init(customView: rightItemView)
        self.navigationItem.rightBarButtonItems = [right1,right2]
    }
    
    func setupSearchHeaderView() -> UIView {
        let headerView:UIView = UIView.init(frame: CGRect.init(x: 30, y: 0, width: Int(Constants.screenWidth) - 145, height: Constants.OrderHeaderSearchViewHeight))
        headerView.layer.masksToBounds = true
        headerView.layer.cornerRadius = CGFloat(Constants.OrderHeaderSearchViewHeight/2)
        headerView.backgroundColor = UIColor.init(hexString: "#F2F2F1")
        headerView.addSubview(self.orderSearchView)
        return headerView
    }
    
    @objc func languageNotification() {
        self.orderScreenSender = nil
        self.orderScreenCollect = nil
        self.menu = nil
        self.pointView.isHidden = true
        self.initItem()
    }
    
    @objc func refreshList(note:Notification)  {
        let noteDict:Dictionary = note.object as! Dictionary<String, Any>
        
        let orderStatue = noteDict["orderStatus"].debugDescription
        //首页刷新
        Constants.window?.hideAllToasts()
        Constants.window?.hideToastActivity()
        allListVC.hideHUDShow = true
        allListVC.tableview.es.startPullToRefresh()
        if orderStatue == Constants.order_cancel {
            cancelOrderVC.hideHUDShow = true
            cancelOrderVC.tableview.es.startPullToRefresh()
        } else if orderStatue == Constants.order_waitColloct {
            waitCollectListVC.hideHUDShow = true
            waitCollectListVC.tableview.es.startPullToRefresh()
            waitSortingVC.hideHUDShow = true
            waitSortingVC.tableview.es.startPullToRefresh()
        } else if orderStatue == Constants.order_waitSorting {
            waitSortingVC.hideHUDShow = true
            waitSortingVC.tableview.es.startPullToRefresh()
            storageSortingVC.hideHUDShow = true
            storageSortingVC.tableview.es.startPullToRefresh()
        } else if orderStatue == Constants.order_storage {
            storageSortingVC.hideHUDShow = true
            storageSortingVC.tableview.es.startPullToRefresh()
            waitSendOrderVC.hideHUDShow = true
            waitSendOrderVC.tableview.es.startPullToRefresh()
        } else if orderStatue == Constants.order_waitDelivery {
            waitDeliveryVC.hideHUDShow = true
            waitDeliveryVC.tableview.es.startPullToRefresh()
            signedVC.hideHUDShow = true
            signedVC.tableview.es.startPullToRefresh()
            stationOrderVC.hideHUDShow = true
            stationOrderVC.tableview.es.startPullToRefresh()
            airportOrderVC.hideHUDShow = true
            airportOrderVC.tableview.es.startPullToRefresh()
        } else if orderStatue == Constants.order_waitReceive {
            cancelOrderVC.hideHUDShow = true
            cancelOrderVC.tableview.es.startPullToRefresh()
            waitTakeOrderVC.hideHUDShow = true
            waitTakeOrderVC.tableview.es.startPullToRefresh()
        }
    }

    func requestActionIsRefresh(refresh:Bool) {
        if currentIndexPage <= controllers.count - 1 {
            let baseC:ZsBaseViewController = controllers[currentIndexPage] as! ZsBaseViewController
            baseC.refreshData()
        }
    }
    
    @objc func refreshOrderTitleCount(note:Notification)  {
        let noteDict:Array = note.userInfo!["orderStatusCounts"] as! Array<JSON>
        var tempAry:Array = Array<String>()
        for titleID in menuStateAry {
            if titleID == Constants.order_All {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_All) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str  == -9 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_All) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_waitColloct {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitCollect) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 3 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitCollect) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_waitSorting {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitSorting) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 5 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitSorting) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_storage {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_Storageing) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 8 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_Storageing) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
                //待派件 15
            } else if titleID == Constants.order_waitDelivery {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_delivery) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 15 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_delivery) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_signed {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_signed) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 16 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_signed) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_station {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_stationed) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 17 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_stationed) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            } else if titleID == Constants.order_air {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_airported) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 18 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_airported) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            }  else if titleID == Constants.order_waitSend {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitSender) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 10 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitSender) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            }  else if titleID == Constants.order_transit {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_carriage) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 13 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_carriage) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            }  else if titleID == Constants.order_waitReceive {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitReceive) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 1 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitReceive) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            }  else if titleID == Constants.order_cancel {
                var title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_canceled) + "0"
                for dicData in noteDict {
                    let dicDataTwo:Dictionary = dicData.dictionary!
                    let str = dicDataTwo["orderStatus"]?.int
                    let str1 = dicDataTwo["orderStatusCount"]?.int
                    if str == 0 {
                        title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_canceled) + String(str1!)
                        break
                    }
                }
                tempAry.append(title)
            }
        }
        self.clickMenuView.refreshDataSourceAction(dataSource: tempAry)
    }
    
    func selectCityCancelDelegate()  {
        self.getOrderScreenSender().removeFromSuperview()
        self.getOrderScreenCollect().removeFromSuperview()
        self.pointView.isHidden = true
    }
    
    func onTapView() {
        self.orderSearchView.resignFirstResponder()
        let scanC = ScanController()
        scanC.scanBlock = { scanStr in
            self.orderSearchView.text = scanStr
            let viewCs:[UIViewController] = self.navigationController!.viewControllers
            for VC in viewCs {
                if VC is OrderViewController {
                    self.navigationController?.popToViewController(VC, animated: true)
                }
            }
            if self.orderSearchView.text?.count ?? 0 > 0 {
                self.isSearched = true
                self.menu = nil
                self.orderScreenSender = nil
                self.orderScreenCollect = nil
                self.pointView.isHidden = true
                self.requestActionIsRefresh(refresh: false)
            } else {
                if self.isSearched {
                    self.isSearched = false
                    self.requestActionIsRefresh(refresh: false)
                }
            }
        }
        self.navigationController?.pushViewController(scanC, animated: false)
    }
    
    func showFiltrateCityView(isSender:String) {
        if isSender == "1" {
            if self.getOrderScreenSender().cityAry.count > 0 {
                Constants.window?.addSubview(self.getOrderScreenSender())
                return
            }
        } else {
            if self.getOrderScreenCollect().cityAry.count > 0 {
                Constants.window?.addSubview(self.getOrderScreenCollect())
                return
            }
        }
        let dic = ["opreateType":isSender,"orderStatus":menuStateAry[currentIndexPage]]
        Constants.window?.makeToast("")
        HttpService.sharedHttpService().getScreenCity(mutDic: dic) { [self] (json, state, error) in
            Constants.window?.hideAllToasts()
            if state {

            var orderSeleectCity:OrderSeleectCity? = nil
            if isSender == "1" {
                Constants.window?.addSubview(self.getOrderScreenSender())
                orderSeleectCity = self.getOrderScreenSender()
            } else {
                Constants.window?.addSubview(self.getOrderScreenCollect())
                orderSeleectCity = self.getOrderScreenCollect()
            }
            orderSeleectCity?.cityAry = json["data"].array
            orderSeleectCity?.isSender = isSender
            orderSeleectCity?.orderStatus = menuStateAry[currentIndexPage]
                
            } else {
                Constants.window?.makeToast(error)
            }
        }
        
    }
    
    @objc func pushPayList(sender:UIButton) {
        orderSearchView.resignFirstResponder()
        if menu == nil {
            menu = CustomPopupMenu()
            menu.layer.shadowColor = UIColor.black.cgColor
            menu.layer.shadowOffset = CGSize.init(width: 2, height: 2)
            menu.layer.shadowOpacity = 0.5
            menu.textColor = UIColor.white
            menu.fillColor = UIColor.init(hexString: "#7F7F7F")
            menu.needBorder = false
            var items:Array<LFPopupMenuItem>!
            
            let item1 = LFPopupMenuItem.createWithTitle(title: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Filtrate_sender), image: UIImage.init(named: "order_triangle_normal")!)
            let item2 = LFPopupMenuItem.createWithTitle(title: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Filtrate_sender), image: UIImage.init(named: "order_triangle_normal")!)
            let item3 = LFPopupMenuItem.createWithTitle(title: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Filtrate_sender), image: UIImage.init(named: "order_triangle_normal")!)
            if orderScreenSender.isSelect || orderScreenCollect.isSelect {
                items = [item1,item2,item3]
            } else {
                items = [item1,item2]
            }
            menu.configWithItems(items: items) { (index) in
                self.popOverView(index:index)
            }
            if orderScreenSender.isSelect {
                let label:UILabel = menu.labelAry[0]
                label.textColor = UIColor.init(hexString: "#FF6E0A")
                let item:UIImageView = menu.imageAry[0]
                item.image = UIImage.init(named: "order_triangle_select")
            } else {
                let label:UILabel = menu.labelAry[0]
                label.textColor = UIColor.init(hexString: "#FFFFFF")
                let item:UIImageView = menu.imageAry[0]
                item.image = UIImage.init(named: "order_triangle_normal")
            }
            if orderScreenCollect.isSelect {
                let label:UILabel = menu.labelAry[1]
                label.textColor = UIColor.init(hexString: "#FF6E0A")
                let item:UIImageView = menu.imageAry[1]
                item.image = UIImage.init(named: "order_triangle_select")
            } else {
                let label:UILabel = menu.labelAry[1]
                label.textColor = UIColor.init(hexString: "#FFFFFF")
                let item:UIImageView = menu.imageAry[1]
                item.image = UIImage.init(named: "order_triangle_normal")
            }
        }
        menu.showArrowToView(view: sender)
    }
     
    
    //pragma mark  ClickMenuDelegate
    
    func clickMenuAction(clickRow: Int) {
        listPageView.scrollToIndex(pageIndex: clickRow, animated: false)
        currentIndexPage = clickRow
        if orderScreenCollect.isSelect || orderScreenSender.isSelect {
            self.requestActionIsRefresh(refresh: true)
        } else {
            self.requestActionIsRefresh(refresh: false)
        }
    }
    
    func selectCityConfirmDelegateIsSender(isSender: String) {
        orderSearchView.text = ""
        if isSender == "1" {
            orderScreenSender.removeFromSuperview()
        } else {
            orderScreenCollect.removeFromSuperview()
        }
        if orderScreenSender.isSelect || orderScreenCollect.isSelect {
            pointView.isHidden = false
        }
        menu = nil
        self.requestActionIsRefresh(refresh: true)
    }
    
    func popOverView(index:Int) {
        switch index {
        case 0:
            self.showFiltrateCityView(isSender: "1")
            break
        case 1:
            self.showFiltrateCityView(isSender: "2")
        break
        case 2:
            menu = nil
            orderScreenCollect = nil
            orderScreenSender = nil
            pointView.isHidden = true
            self.requestActionIsRefresh(refresh: true)
        default:
            break
        }
    }
    
    func getOrderStr() -> Array<String> {
        var stateStrAry = Array<String>()
        for strID in menuStateAry {
            if strID == Constants.order_All {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_All))
            } else if (strID == Constants.order_waitReceive) {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitReceive))
            } else if (strID == Constants.order_waitColloct) {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitCollect))
            } else if (strID == "5") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitSorting))
            } else if (strID  == "8") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_Storageing))
            } else if (strID  == "15") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_delivery))
            } else if (strID  == "16") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_signed))
            } else if (strID  == "17") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_stationed))
            } else if (strID  == "18") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_airported))
            } else if (strID  == "0") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_canceled))
            } else if (strID  == Constants.order_waitSend) {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitSender))
            } else if (strID  == "13") {
                stateStrAry.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_carriage))
            }
        }
        return stateStrAry;
    }
    
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //zs待处理
//        if ([scrollView isKindOfClass:[UITableView class]])
//        {
//            return;
//        }
        let index:Int = Int(scrollView.contentOffset.x/scrollView.frame.size.width)
        self.clickMenuView.selectDidMenuAction(menuTag: index + 100)
        
        currentIndexPage = index
        if orderScreenCollect.isSelect || orderScreenSender.isSelect {
            self.requestActionIsRefresh(refresh: true)
        } else {
            self.requestActionIsRefresh(refresh: false)
        }
    }
    
    func cellAction(actionType:OrderActionType,orderModel:OrderListModel,titleStr:NSString) {
        if actionType == OrderActionType.Order_LogisticsMsg_Type {
            let trackC = OrderTrackController()
            trackC.mailSn = orderModel.mailSn
            self.navigationController?.pushViewController(trackC, animated: true)
        } else if (actionType == OrderActionType.Order_Cancel_Type || actionType == OrderActionType.Order_Delete_Type) {
            var str = ""
            if actionType == OrderActionType.Order_Cancel_Type  {
                str = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_affirmCancel)
            } else {
                str = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_affirmDelete)
            }
            let alertt = UIAlertController.init(title: str, message: "", preferredStyle: .alert)
            let cencel = UIAlertAction.init(title: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_cancel), style: .default) { (action) in
                
            }
            let confirm = UIAlertAction.init(title: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), style: .default) { (action) in
                Constants.window?.makeToast("")
                if actionType == OrderActionType.Order_Cancel_Type {
                    
                } else {
                    
                }
            }
            confirm.setValue(UIColor.init(hexString: "#000000"), forKey: "titleTextColor")
            alertt.addAction(cencel)
            alertt.addAction(confirm)
            self.present(alertt, animated: true) {
                
            }
        } else if (actionType == OrderActionType.Order_Print_Type) {
            
        } else if (actionType == OrderActionType.Order_Edit_Type) {
            
        } else if (actionType == OrderActionType.Order_Collect_Type) {
            
        } else if (actionType == OrderActionType.Order_Sorting_Type) {
            
        } else if (actionType == OrderActionType.Order_EnterLibrary_Type) {

        } else if (actionType == OrderActionType.Order_LeaveLibrary_Type) {

        } else if (actionType == OrderActionType.Order_Repel_Type) {
            
        } else if (actionType == OrderActionType.Order_Sign_Type) {
            
        } else if (actionType == OrderActionType.Order_callSenderPhone_Type) {
            
            let telURL:URL = URL.init(string: "tel:"+orderModel.cwPhone)!
            UIApplication.shared.open(telURL, options:[:]) { (state) in
                
            }
        }

    }
    
    lazy var clickMenuView: ClickMenu = {
        let clickMenuView = ClickMenu.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: 40), itemNum: 0)
        clickMenuView.menuDelegate = self
        clickMenuView.backgroundColor = UIColor.white
        clickMenuView.normalColor = UIColor.init(hexString: "#000000")
        clickMenuView.selectColor = UIColor.init(hexString: "#FF6E0A")
        clickMenuView.menuFont = 14
        clickMenuView.selectMenuFont = 14
        clickMenuView.showsHorizontalScrollIndicator = false
        return clickMenuView
    }()
    
    lazy var listPageView : ListPageView = {
        let listPageView = ListPageView.init(frame: CGRect.init(x: 0, y: 57, width: Constants.screenWidth, height: Constants.screenHeight - Constants.NavagitionHeight - CGFloat(57) - Constants.kTabBarHeight), type: SCROL_TYPE.H_SCROL)
        listPageView.delegate = self
        return listPageView
    }()
    
    lazy var orderSearchView: OrderSearchView = {
        let orderSearchView = OrderSearchView.init(frame: CGRect.init(x: 5, y: 0, width: Constants.screenWidth - CGFloat(165), height:CGFloat(Constants.OrderHeaderSearchViewHeight)))
        orderSearchView.isUserInteractionEnabled = true

        return orderSearchView
    }()
    func getOrderScreenSender() -> OrderSeleectCity {
        if (orderScreenSender == nil) {
            orderScreenSender = OrderSeleectCity.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
            orderScreenSender.delegate = self
            orderScreenSender.isSender = "1"
        }
        return orderScreenSender
    }
    
    func getOrderScreenCollect() -> OrderSeleectCity {
        if (orderScreenCollect == nil) {
            self.orderScreenCollect = OrderSeleectCity.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
            orderScreenCollect.delegate = self
            orderScreenCollect.isSender = "2"
        }
        return orderScreenCollect
    }
    
    lazy var seleectDestination: OrderDestinationCity = {
        let seleectDestination = OrderDestinationCity.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
        return seleectDestination
    }()


    
}
